﻿Imports System.Data.SqlClient
Public Class FrmDanhMucPhong
    

    Dim cnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True"

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub FrmDanhMucPhong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        Dim bs As New BindingSource()
        Dim sql As String = "select * from Phong"
        Dim con As New SqlConnection(cnStr)
        Dim da As New SqlDataAdapter(sql, con)
        da.Fill(dt)
        bs.DataSource = dt
        dgvDanhMucPhong.DataSource = bs
        LoadCbx()
    End Sub

    Private Sub btnThemPhong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemPhong.Click

        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "insert into Phong(TenPhong,MaLoaiPhong,GhiChu) values(@TenPhong,@MaLoaiPhong,@GhiChu)"
        Dim cmd As New SqlCommand(sql, con)

        cmd.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 10)
        cmd.Parameters("@TenPhong").Value = txtTenP.Text

        cmd.Parameters.Add("@MaLoaiPhong", SqlDbType.VarChar, 10)
        cmd.Parameters("@MaLoaiPhong").Value = cbxLoai.SelectedValue

        cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar)
        cmd.Parameters("@GhiChu").Value = txtGhiChu.Text

        cmd.Connection.Open()

        Dim i As Integer = cmd.ExecuteNonQuery() 'thuc thi lenh
        If (i > 0) Then
            MessageBox.Show("Đã thêm phòng")
        Else
            MessageBox.Show("Xảy ra lỗi!!")
        End If
        cmd.Connection.Close()
        FrmDanhMucPhong_Load(sender, e)
    End Sub



    Private Sub btnCancel_Click(ByVal sende As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        txtGhiChu.Text = ""
        txtTenP.Text = ""
    End Sub



    Private Sub LoadCbx()
        Dim con As New SqlConnection(cnStr)
        Dim sql2 As String = "select MaLoaiPhong from LoaiPhong"
        Dim da2 As New SqlDataAdapter(sql2, con)
        Dim dt2 As New DataTable()
        da2.Fill(dt2)
        cbxLoai.DataSource = dt2
        cbxLoai.DisplayMember = "MaLoaiPhong"
        cbxLoai.ValueMember = "MaLoaiPhong"
    End Sub




    Private Sub dgvDanhMucPhong_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDanhMucPhong.CellClick
        If (e.RowIndex >= 0 And e.ColumnIndex >= 0) Then
            txtTenP.Text = Convert.ToString(dgvDanhMucPhong.CurrentRow.Cells("TenPhong").Value)
            cbxLoai.SelectedValue = Convert.ToString(dgvDanhMucPhong.CurrentRow.Cells("MaLoaiPhong").Value.ToString())
            txtGhiChu.Text = Convert.ToString(dgvDanhMucPhong.CurrentRow.Cells("GhiChu").Value)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "delete from Phong where TenPhong=@TenPhong"
        Dim cmd As New SqlCommand(sql, con)

        cmd.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 10)
        cmd.Parameters("@TenPhong").Value = txtTenP.Text
        cmd.Connection.Open()
        Dim i As Integer = cmd.ExecuteNonQuery() 'thuc thi lenh
        If (i > 0) Then
            MessageBox.Show("Đã xóa phòng")
        Else
            MessageBox.Show("Xảy ra lỗi!!")
            FrmDanhMucPhong_Load(sender, e)
        End If
        cmd.Connection.Close()
        dgvDanhMucPhong.Rows.Remove(dgvDanhMucPhong.CurrentRow)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "update Phong  set TenPhong=@TenPhong,MaLoaiPhong=@MaLoaiPhong,GhiChu=@GhiChu where MaPhong=@MaPhong"
        Dim cmd As New SqlCommand(sql, con)


        cmd.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 10)
        cmd.Parameters("@TenPhong").Value = txtTenP.Text


        cmd.Parameters.Add("@MaLoaiPhong", SqlDbType.NVarChar, 10)
        cmd.Parameters("@MaLoaiPhong").Value = cbxLoai.SelectedValue


        cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar, 255)
        cmd.Parameters("@GhiChu").Value = txtGhiChu.Text

        cmd.Parameters.Add("@MaPhong", SqlDbType.Int, 10)
        cmd.Parameters("@MaPhong").Value = dgvDanhMucPhong.CurrentRow.Cells("MaPhong").Value


        cmd.Connection.Open()
        Dim i As Integer = cmd.ExecuteNonQuery() 'thuc thi lenh
        If (i > 0) Then
            MessageBox.Show("Đã sửa thông tin phòng")
        Else
            MessageBox.Show("Xảy ra lỗi!!")
        End If
        cmd.Connection.Close()
        FrmDanhMucPhong_Load(sender, e)
    End Sub

    Private Sub dgvDanhMucPhong_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvDanhMucPhong.CellFormatting
        If Me.dgvDanhMucPhong.Columns(e.ColumnIndex).Name = "TrangThai" Then
            If e.Value IsNot Nothing Then
                Dim stringvalue As String = _
                    CType(e.Value, String)
                If (stringvalue.IndexOf("Còn Trống") > -1) Then
                    e.CellStyle.BackColor = Color.Green
                    e.CellStyle.ForeColor = Color.White
                ElseIf (stringvalue.IndexOf("Đã Thuê") > -1) Then
                    dgvDanhMucPhong.CurrentRow.ReadOnly = True
                End If
            End If
            End If
    End Sub

   
   

End Class